package com.example.string;

import org.junit.Test;

/**
 * @author readpage
 * @create 2022-09-07 21:24
 */
public class StringDemo3 {
    /**
     * 获取两个字符串中最大相同子串。比如:
     * str1 = "abcwerthelloyuiodefabcdef"; str2 = "cvhellobnm"
     * 提示: 将短的那个串进行长度依次递减的子串与较长的串比较。
     */

    private String getMaxSameString(String str1, String str2) {
        String maxStr = (str1.length() >= str2.length()) ? str1 : str2;
        String minStr = (str1.length() < str2.length()) ? str1 : str2;
        int length = minStr.length();

        for (int i = 0; i < length; i++) {
            for (int x = 0, y = length - i;;) {
                String subStr = minStr.substring(x, y);
                if (maxStr.contains(subStr)) {
                    return subStr;
                }
            }
        }
        return null;
    }

    @Test
    public void testGetMaxSameString() {
        String str1 = "abcwerthelloyuiodefabcdef";
        String str2 = "cvhellobnm";
        String maxSameString = getMaxSameString(str1, str2);
        System.out.println(maxSameString);
    }
}
